package com.mechanatrix.terrain;

import com.jme3.app.SimpleApplication;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.util.SkyFactory;
import com.jme3.util.TangentBinormalGenerator;

/**
 * test
 *
 * @author normenhansen
 */
public class CubeMain extends SimpleApplication {

	public static void main(String[] args) {
		CubeMain app = new CubeMain();
		app.start();
	}

	@Override
	public void simpleInitApp() {

		// Add sky
		//rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));

		// Create a box with a texture
		Box box05 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box05);
		Spatial spat05 = new Geometry("Box", box05);
		spat05.setLocalTranslation(-30.0f, -2.5f, 0.0f);
		spat05.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat05 = assetManager.loadMaterial("MatDefs/metal_plates_05.j3m");
		spat05.setMaterial(mat05);
		rootNode.attachChild(spat05);

		// Create a box with a texture
		Box box06 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box06);
		Spatial spat06 = new Geometry("Box", box06);
		spat06.setLocalTranslation(-20.0f, -2.5f, 0.0f);
		spat06.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat06 = assetManager.loadMaterial("MatDefs/metal_plates_06.j3m");
		spat06.setMaterial(mat06);
		rootNode.attachChild(spat06);

		// Create a box with a texture
		Box box08 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box08);
		Spatial spat08 = new Geometry("Box", box08);
		spat08.setLocalTranslation(-10.0f, -2.5f, 0.0f);
		spat08.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat08 = assetManager.loadMaterial("MatDefs/metal_plates_08.j3m");
		spat08.setMaterial(mat08);
		rootNode.attachChild(spat08);

		// Create a box with a texture
		Box box12 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box12);
		Spatial spat12 = new Geometry("Box", box12);
		spat12.setLocalTranslation(0.0f, -2.5f, 0.0f);
		spat12.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat12 = assetManager.loadMaterial("MatDefs/metal_plates_12.j3m");
		spat12.setMaterial(mat12);
		rootNode.attachChild(spat12);

		// Create a box with a texture
		Box box13 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box13);
		Spatial spat13 = new Geometry("Box", box13);
		spat13.setLocalTranslation(10.0f, -2.5f, 0.0f);
		spat13.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat13 = assetManager.loadMaterial("MatDefs/metal_plates_13.j3m");
		spat13.setMaterial(mat13);
		rootNode.attachChild(spat13);

		// Create a box with a texture
		Box box18 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box18);
		Spatial spat18 = new Geometry("Box", box18);
		spat18.setLocalTranslation(20.0f, -2.5f, 0.0f);
		spat18.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat18 = assetManager.loadMaterial("MatDefs/metal_plates_18.j3m");
		spat18.setMaterial(mat18);
		rootNode.attachChild(spat18);

		// Create a box with a texture
		Box box19 = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box19);
		Spatial spat19 = new Geometry("Box", box19);
		spat19.setLocalTranslation(30.0f, -2.5f, 0.0f);
		spat19.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material mat19 = assetManager.loadMaterial("MatDefs/metal_plates_15.j3m");
		spat19.setMaterial(mat19);
		rootNode.attachChild(spat19);

		// Add sun
		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
		rootNode.addLight(sun);
	}

	public void setTextureScale(Spatial spatial, Vector2f vector) {
		if (spatial instanceof Node) {
			Node findingnode = (Node) spatial;
			for (int i = 0; i < findingnode.getQuantity(); i++) {
				Spatial child = findingnode.getChild(i);
				setTextureScale(child, vector);
			}
		} else if (spatial instanceof Geometry) {
			((Geometry) spatial).getMesh().scaleTextureCoordinates(vector);
		}
	}

	protected void setTextureOfObject(Spatial spatial) {
		Texture tex = assetManager.loadTexture("Models/white-tile-texture.jpg");
		tex.setWrap(WrapMode.Repeat);

		Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat1.setTexture("ColorMap", tex);

		setTextureScale(spatial, new Vector2f(2, 2));

		spatial.setMaterial(mat1);
	}

	@Override
	public void simpleUpdate(float tpf) {
		//TODO: add update code
	}

	@Override
	public void simpleRender(RenderManager rm) {
		//TODO: add render code
	}
}
